﻿using System;
using System.Windows.Input;
using Azukit.WPF.Command;
using Azukit.WPF.ViewModel;
using Hibiku.Helpers;
using Hibiku.Twitter;
using Hibiku.Views;

namespace Hibiku.ViewModels
{
    public class TweetViewModel : ViewAwareViewModelBase
    {
        public Tweet Tweet
        {
            get;
            private set;
        }

        private bool m_isHighlighted;
        public bool IsHighlighted
        {
            get { return m_isHighlighted; }
            set
            {
                if (value != m_isHighlighted)
                {
                    m_isHighlighted = value;

                    FirePropertyChanged(() => IsHighlighted);
                }
            }
        }

        public bool IsNew
        {
            get { return DateTime.Now.Subtract(Tweet.CreatedAt).TotalHours < 1; }
        }

        public bool IsRetweet
        {
            get { return Tweet.Retweeted != null; }
        }

        public bool HasLocation
        {
            get { return Tweet.Location != null; }
        }

        public string LocationName
        {
            get
            {
                if (!HasLocation)
                {
                    return "";
                }
                else
                {
                    if (Tweet.Location.Place != null)
                    {
                        return "★ " + Tweet.Location.Place;
                    }
                    else
                    {
                        return "★ Here";
                    }
                }
            }
        }

        public ICommand ShowUserInformationCommand
        {
            get;
            private set;
        }

        public ICommand ShowInBrowserCommand
        {
            get;
            private set;
        }

        public ICommand ShowReplyViewCommand
        {
            get;
            private set;
        }

        public ICommand ShowLocationCommand
        {
            get;
            private set;
        }

        private readonly ITwitterApi m_api;

        public TweetViewModel(Tweet tweet, bool isHighlighted, ITwitterApi api)
        {
            m_api = api;

            Tweet = tweet;
            IsHighlighted = isHighlighted;

            ShowUserInformationCommand = new DelegatingCommand(x => DoShowUserInformation());
            ShowInBrowserCommand = new DelegatingCommand(x => DoShowInBrowser());
            ShowReplyViewCommand = new DelegatingCommand(x => DoShowReplyView());
            ShowLocationCommand = new DelegatingCommand(x => DoShowLocation());
        }

        private void DoShowUserInformation()
        {
            var url = string.Format("http://twitter.com/{0}", Tweet.User.ScreenName);

            UrlHelper.OpenUrl(url);
        }

        private void DoShowInBrowser()
        {
            var url = string.Format("http://twitter.com/{0}/status/{1}", Tweet.User.ScreenName, Tweet.Id);

            UrlHelper.OpenUrl(url);
        }

        private void DoShowReplyView()
        {
            var view = new ReplyView();
            var viewModel = view.DataContext as ReplyViewModel;
            viewModel.Tweet = this;
            view.Show();
        }

        private void DoShowLocation()
        {
            UrlHelper.OpenLocation(Tweet.Location);
        }
    }
}
